<?php
function create_wordpress_admin_content_scroller_submenu()
{
	add_submenu_page( 'my-top-level-handle', 'Content Scroller', 'Content Scroller', 'manage_options', 'my-submenu-content-scroller-handle', 'content_scroller_admin_page');
	add_action('admin_init', 'register_content_scroller_settings');
}
	
function register_content_scroller_settings()
{ 
	register_setting('content_scroller_options', 'content_scroller_active');
	register_setting('content_scroller_options', 'content_scroller_categories');
	register_setting('content_scroller_options', 'content_scroller_elements_to_scroll');
	register_setting('content_scroller_options', 'content_scroller_elements_link');
	register_setting('content_scroller_options', 'content_scroller_title');
	register_setting('content_scroller_options', 'content_scroller_description');
}

if ( isset($_POST['content_scroller_settings_update']) && $_POST['content_scroller_settings_update'] == 'true')
	content_scroller_settings_update();
	
function content_scroller_settings_update()
{
	update_option( 'content_scroller_active', $_POST['content_scroller_active'] );
	if ( isset($_POST['content_scroller_categories']) ) $content_scroller_categories = implode(',' ,$_POST['content_scroller_categories']);
		else $content_scroller_categories = '';
	update_option( 'content_scroller_categories', $content_scroller_categories );
	update_option( 'content_scroller_elements_to_scroll', $_POST['content_scroller_elements_to_scroll'] );
	update_option( 'content_scroller_elements_link', $_POST['content_scroller_elements_link'] );
	update_option( 'content_scroller_title', stripslashes($_POST['content_scroller_title']) );
	update_option( 'content_scroller_description', stripslashes($_POST['content_scroller_description']) );
}
	
function content_scroller_admin_page() {
?>
	<script type="text/javascript" language="javascript">
		/* function for confirming you want to delete */
		function confirmAction()
		{
			return confirm("Are you sure you want to delete this slide?")
		}
		
		jQuery(document).ready(function() {
			jQuery('.header').click(function() {
				jQuery(this).next('.hidden_content').stop(true, true).toggle('fast');
			});
		});
		
		jQuery(document).ready(function() {
			jQuery("#content_scroller_tabs").tabs();
			jQuery(".transform").jqTransform();
		});
	</script>
    
<div class="wrap">
	<div id="icon-options-general" class="icon32"><br /></div>
	<h2>Content Scroller Options Panel</h2>
    
     <?php
	$get_admin_warnings = admin_warnings();
	if ( $get_admin_warnings != '' )
	{
		echo '<div id="admin_warnings">';
			echo '<div id="admin_warning_icon"></div>';
			echo $get_admin_warnings;
		echo '</div>';
	}
	?>
    
<div id="content_scroller_tabs" class="admin_tabs">
    <ul>
        <li>
            <a href="#">
                Settings
            </a>
        </li>
</div>
    
    <form method="post" action="#" class="transform">
    	<input type="hidden" name="content_scroller_settings_update" value="true" />
		<?php settings_fields('content_scroller_options'); ?>
        
        <div class="settings_form">
            <div class="settings_wrapper">
                <div class="header">
                    Content Scroller
                </div>
                <div class="row_data">
                	<?php $content_scroller_active = get_option('content_scroller_active'); if ( $content_scroller_active == '' ) $content_scroller_active = 'on';  ?>
                	<div class="row clearfix alternate">
                        <label>Enable Scroller</label>
                        <select name="content_scroller_active" class="general_options_select">
                            <option value="on" <?php if ($content_scroller_active == 'on') { echo 'selected="selected"'; } ?>>On</option>
                            <option value="front_page_only" <?php if ($content_scroller_active == 'front_page_only') { echo 'selected="selected"'; } ?>>Front Page Only</option>
                            <option value="off" <?php if ($content_scroller_active == 'off') { echo 'selected="selected"'; } ?>>Off</option>
                        </select>
                    </div>
                    
                    <div class="row clearfix">
                        <label>Categories</label>
                        <ul class="categories_listing">
							<?php
							global $wpdb;
							
							$current_categories = explode(',', get_option( 'content_scroller_categories' ));
							$categories = get_categories();
							
							foreach ($categories as $category)
							{
								$validate = NULL;
								$checked = '';
								
								$haystack = array();
								$haystack[] = $category->cat_ID;
								
								foreach($current_categories as $current_category)
								{
									if ($current_category == $category->cat_ID)
									{
										$validate = true;
										break;
									}
								}
	
								if ( $validate != '' ) $checked = ' checked="checked"';
								echo '<li>';
									echo '<input id="post_'.$category->cat_ID.'" type="checkbox" name="content_scroller_categories[]" value="'.$category->cat_ID.'"'.$checked.' />';
									// cred ca va trebui schimbat cat id
									//echo '<label for="scroller-category-'.$category->cat_ID.'">'.
									echo $category->name;
								echo '</li>';										
							}
							
                        	?>
                        </ul>
                    </div>
				
                	<div class="row clearfix alternate">
                    	<label>Nr of elements to scroll:</label>
                        <select name="content_scroller_elements_to_scroll" class="general_options_select">
                             <option value="1" <?php if ( get_option('content_scroller_elements_to_scroll', '2') == '1') { echo 'selected="selected"'; } ?>>1</option>
                             <option value="2" <?php if ( get_option('content_scroller_elements_to_scroll', '2') == '2') { echo 'selected="selected"'; } ?>>2</option>
                             <option value="3" <?php if ( get_option('content_scroller_elements_to_scroll', '2') == '3') { echo 'selected="selected"'; } ?>>3</option>
                             <option value="4" <?php if ( get_option('content_scroller_elements_to_scroll', '2') == '4') { echo 'selected="selected"'; } ?>>4</option>
                        </select>
                    </div>

           			<?php $content_scroller_elements_link = get_option('content_scroller_elements_link', 'gallery'); ?>
                    <div class="row clearfix">
                        <label>Links go to:</label>
                        <select name="content_scroller_elements_link" class="general_options_select">
                             <option value="image" <?php if ($content_scroller_elements_link == 'image') { echo 'selected="selected"'; } ?>>Single Image</option>
                             <option value="gallery" <?php if ($content_scroller_elements_link == 'gallery') { echo 'selected="selected"'; } ?>>Lightbox Gallery</option>
                             <option value="permalink" <?php if ($content_scroller_elements_link == 'permalink') { echo 'selected="selected"'; } ?>>Permalink</option>
                        </select>
                    </div>
					
                    <div class="row clearfix alternate">
                    	<label>Title:</label>
                        <input type="text" name="content_scroller_title" class="inputbox add_slide_input input_box" value="<?php echo get_option('content_scroller_title');?>" />
                    </div>
                    
                    <div class="row clearfix">
                    	<label>Description:</label>
                        <textarea rows="5" cols="200;" name="content_scroller_description" class="add_slide_input input_box"><?php echo get_option('content_scroller_description');?></textarea>
                    </div>
                    
            		<div class="row clearfix alternate">
	                    <input name="Submit" type="submit" class="save_button" value="<?php esc_attr_e('Save Changes'); ?>" />
					</div>
            
            	</div> <!-- end .row_data -->
                <div class="footer">
                	Content Scroller
                </div>
            </div> <!-- end .settings_wrapper" -->
    	</div> <!-- end .settings_form -->
	</form>

</div> <!--end of .wrap -->  
<?php
} // END content_scroller_admin_page()
?>